<!--#
layout("/layouts/platform.html"){
#-->
<style>
    .el-upload input[type=file] {
        display: none;
    }

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 128px;
        height: 128px;
        line-height: 128px;
        text-align: center;
    }

    .avatar {
        width: 128px;
        height: 128px;
        display: block;
    }
</style>
<link href="${base!}/assets/platform/plugins/umeditor/themes/default/css/umeditor.min.css" type="text/css"
      rel="stylesheet">
<script src="${base!}/assets/platform/plugins/umeditor/umeditor.config.js"></script>
<script src="${base!}/assets/platform/plugins/umeditor/umeditor.js"></script>
<div id="app" v-cloak>
    <el-row class="header navbar bg-white shadow">
        <div class="btn-group tool-button mt5">
            <el-input v-model="pageForm.searchKeyword" @keyup.enter.native="doSearch" placeholder="请输入活动名称"
                      size="medium"></el-input>
        </div>
        <div class="btn-group tool-button mt5">
            <el-select
                    v-model="pageForm.language"
                    filterable
                    remote
                    reserve-keyword
                    placeholder="选择语言版本">
                <el-option
                        v-for="item in languages"
                        :key="item.code"
                        :label="item.name"
                        :value="item.code">
                </el-option>
            </el-select>
        </div>
        <div class="btn-group tool-button mt5">
            <el-select v-model="pageForm.isLongActivity"
                       size="medium"
                       placeholder="请选择活动时长类型">
                <el-option v-for="item in typeData"
                           :value="item.val"
                           :label="item.name">
                </el-option>
            </el-select>
        </div>
        <div class="btn-group tool-button mt5">
            <el-date-picker
                    v-model="searchDate"
                    type="datetimerange"
                    :picker-options="pickerOptions"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    align="right"
                    size="medium"
                    :default-time="['00:00:00', '23:59:59']"
                    value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
        </div>
        <!--搜索按钮-->
        <div class="btn-group tool-button mt5">
            <el-button slot="append" icon="el-icon-search" @click="doSearch" size="medium"></el-button>
        </div>
        <!--新增按钮-->
        <div class="pull-right offscreen-right mt5">
            <el-button size="medium" @click="openAdd"><i class="ti-plus"></i>添加活动</el-button>
        </div>
    </el-row>
    <el-row class="el-table-container">
        <el-table
                :data="tableData"
                @sort-change='pageOrder'
                size="small"
                header-align="center"
                :default-sort="{prop: 'opAt', order: 'descending'}"
                style="width: 100%">

            <el-table-column
                    prop="remark"
                    label="活动名称"
                    header-align="center"
                    align="center"
                    :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column
                    prop="picture"
                    label="图片"
                    header-align="center"
                    align="center">
                <template slot-scope="scope">
                    <p v-if="scope.row.picture">
                        <img :src="scope.row.picture" style="width: 60px;height: 60px">
                    </p>
                    <p v-else>
                        暂无数据
                    </p>
                </template>
            </el-table-column>


<!--            <el-table-column-->
<!--                    prop="language"-->
<!--                    label="语言"-->
<!--                    header-align="center"-->
<!--                    align="center"-->
<!--                    :show-overflow-tooltip="true">-->
<!--            </el-table-column>-->

            <el-table-column
                    prop="language"
                    label="语言版本"
                    header-align="center"
                    align="center"
                    :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    {{languageName(scope.row.language)}}
                </template>
            </el-table-column>

            <el-table-column
                    prop="activityName"
                    label="用户端活动名称"
                    header-align="center"
                    align="center"
                    :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column
                    prop="isLongActivity"
                    label="活动时长"
                    header-align="center"
                    align="center"
                    :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <p v-if="scope.row.isLongActivity">
                        长期活动
                    </p>
                    <p v-else>
                        {{scope.row.startDate}}至{{scope.row.endDate}}
                    </p>
                </template>
            </el-table-column>

            <el-table-column
                    prop="disabled"
                    label="状态"
                    header-align="center"
                    align="center">
                <template slot-scope="scope">
                    <i v-if="!scope.row.disabled" class="fa fa-circle text-success ml5"></i>
                    <i v-if="scope.row.disabled" class="fa fa-circle text-danger ml5"></i>
                </template>
            </el-table-column>

            <el-table-column
                    prop="ctAt"
                    label="创建时间"
                    header-align="center"
                    align="center"
                    width="120px">
                <template slot-scope="scope">
                    {{formatAt(scope.row.ctAt)}}
                </template>
            </el-table-column>

            <el-table-column
                    label="操作"
                    width="120"
                    align="center">
                <template slot-scope="scope">
                    <el-dropdown @command="dropdownCommand">
                        <el-button size="mini">
                            <i class="ti-settings"></i>
                            <span class="ti-angle-down"></span>
                        </el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item v-if="scope.row.disabled"
                                              :command="{type:'disable', id:scope.row.id, status: 0}">
                                启用
                            </el-dropdown-item>
                            <el-dropdown-item v-else-if="!scope.row.disabled"
                                              :command="{type:'disable', id:scope.row.id, status: 1}">
                                禁用
                            </el-dropdown-item>
                            <el-dropdown-item divided :command="{type:'edit', id:scope.row.id, row:scope.row}">
                                修改
                            </el-dropdown-item>
                            <el-dropdown-item divided :command="{type:'delete',id:scope.row.id}">
                                删除
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </template>
            </el-table-column>
        </el-table>
    </el-row>
    <!--    分页-->
    <el-row class="el-pagination-container">
        <el-pagination
                @size-change="pageSizeChange"
                @current-change="pageNumberChange"
                :current-page="pageForm.pageNumber"
                :page-sizes="[10, 20, 30, 50]"
                :page-size="pageForm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageForm.totalCount">
        </el-pagination>
    </el-row>

    <!--    新建/修改 Banner弹窗-->
    <el-dialog
            :title="titlePopu==0?'添加活动':'修改活动'"
            :visible.sync="editDialogVisible"
            :close-on-click-modal="false"
            width="60%">
        <el-form :model="formData" ref="editForm" :rules="formRules" size="small" label-width="200px">

            <el-form-item prop="remark" label="活动名称">
                <el-input v-model="formData.remark" placeholder="仅后台可见">
                </el-input>
            </el-form-item>
<!--            <el-form-item prop="language" label="语言">-->
<!--                <el-input v-model="formData.language" placeholder="语言请以英文逗号分隔">-->
<!--                </el-input>-->
<!--            </el-form-item>-->

            <el-form-item prop="language" label="语言版本">
                <el-select
                        v-model="formData.language">
                    <el-option
                            v-for="item in languages"
                            :key="item.code"
                            :label="item.name"
                            :value="item.code">
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item prop="activityName" label="活动名称">
                <el-input maxlength="100" placeholder="活动名称（对应语言版本下活动名称-用户端展示）"
                          v-model="formData.activityName"
                          auto-complete="off" tabindex="2" type="text"></el-input>
            </el-form-item>

            <el-form-item prop="picture" label="图片">
                <el-upload
                        class="avatar-uploader"
                        tabindex="5"
                        action="${base}/open/file/upload/image2"
                        name="Filedata"
                        :show-file-list="false"
                        :before-upload="beforeUpload"
                        :on-success="function(resp,file,fileList){return handlePicSuccess(resp,file,fileList,'picture')}">
                    <img v-if="formData.picture" :src="formData.picture" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>

            <el-form-item prop="isLongActivity" label="活动时长(不选为短期活动)">
                <el-switch
                        v-model="formData.isLongActivity"
                        active-color="#13ce66"
                        inactive-color="#909399"
                        @change="activityChange">
                </el-switch>
            </el-form-item>

            <el-form-item v-show="isShow" prop="editSearchDate" label="起止时间">
                <el-date-picker
                        v-model="editSearchDate"
                        type="daterange"
                        :picker-options="editPickerOptions"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        unlink-panels
                        align="right"
                        size="medium"
                        value-format="yyyy-MM-dd">
                </el-date-picker>
            </el-form-item>

            <el-form-item prop="content" label="内容">
                <script type="text/plain" id="content" class="note"></script>
            </el-form-item>
        </el-form>
        <!-- 提交按钮 -->
        <span slot="footer" class="dialog-footer">
                    <el-button @click="editDialogVisible = false, isShow = true">取 消</el-button>
                    <el-button type="primary" @click="doAdd">确 定</el-button>
        </span>
    </el-dialog>
</div>
<script>
    new Vue({
        el: "#app",
        data: function () {
            let self = this;
            return {
                pickerOptions: {
                    shortcuts: [{
                        text: '最近一周',
                        onClick: function (picker) {
                            let end = new Date();
                            let start = new Date();
                            let temp = moment().add(-1, 'weeks').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick: function (picker) {
                            let end = new Date();
                            let start = new Date();
                            let temp = moment().add(-1, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick: function (picker) {
                            let end = new Date();
                            let start = new Date();
                            let temp = moment().add(-3, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
                editPickerOptions: {
                    disabledDate: function (time) {
                        return time.getTime() < new Date(new Date(new Date().toLocaleDateString()).getTime());
                    }
                },
                typeData: [{
                    val: '',
                    name: '全部'
                }, {
                    val: 0,
                    name: '短期活动'
                }, {
                    val: 1,
                    name: '长期活动'
                }],
                titlePopu: 0,
                editDialogVisible: false,
                tableData: [],
                pageForm: {
                    searchKeyword: "",
                    searchDate: "",
                    isLongActivity: "",
                    pageNumber: 1,
                    pageSize: 10,
                    totalCount: 0,
                },
                formData: {
                    activityName: "",
                    picture: "",
                    isLongActivity: false,
                    startDate: "",
                    endDate: "",
                    content: ""
                },
                isShow: true,
                formRules: {
                    activityName: [
                        {required: true, message: '请输入活动名称', trigger: 'blur'}
                    ],
                    picture: [
                        {required: true, message: '请上传图片', trigger: 'blur'}
                    ]
                },
                searchDate: [],
                editSearchDate: [],
                fileType: ['jpg', 'png', 'jpeg', 'gif'],
                // languages:["en","th","hi"]
                languages: [{code:"en",name:"英"}, {code:"th",name:"泰"}, {code:"hi",name:"印"},{code:"id",name:"印尼"}]
            }
        },
        methods: {
            languageName: function(val){
                for (let i = 0; i <this.languages.length; i++) {
                    if (this.languages[i].code == val) return this.languages[i].name
                }
                return val
            },
            formatAt: function (val) {
                if (val > 0)
                    return moment(val * 1000).format("YYYY-MM-DD HH:mm");
                return "";
            },
            pageOrder: function (column) {//按字段排序
                this.pageForm.pageOrderName = column.prop;
                this.pageForm.pageOrderBy = column.order;
                this.pageData();
            },
            pageNumberChange: function (val) {//页码更新操作
                this.pageForm.pageNumber = val;
                this.pageData();
            },
            pageSizeChange: function (val) {//分页大小更新操作
                this.pageForm.pageSize = val;
                this.pageData();
            },
            pageData: function () {//加载分页数据
                let self = this;
                sublime.showLoadingbar();//显示loading
                if (self.searchDate) {
                    self.pageForm.searchDate = self.searchDate.toString();
                } else {
                    self.pageForm.searchDate = "";
                }
                $.post(base + "/platform/cms/activity/data", self.pageForm, function (data) {
                    sublime.closeLoadingbar();//关闭loading
                    if (data.code === 0) {
                        self.tableData = data.data.list;
                        self.pageForm.totalCount = data.data.totalCount;
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            formatAt: function (val) {
                if (val > 0)
                    return moment(val * 1000).format("YYYY-MM-DD HH:mm");
                return "";
            },
            doSearch: function () {
                this.pageForm.pageNumber = 1;
                this.pageData();
            },
            openAdd: function () {
                let self = this;
                self.titlePopu = 0;
                self.editDialogVisible = true;
                //打开新增窗口,表单先清空
                if (self.$refs["editForm"]) {
                    self.$refs["editForm"].resetFields();
                }
                self.formData = {isLongActivity: false,language:self.languages[0].code};
                self.editSearchDate = [];
                UM.getEditor("content");
                setTimeout(function () {
                    UM.getEditor("content").setContent("", false);
                }, 200);
            },
            // 提交按钮逻辑
            doAdd: function () {
                let self = this;
                self.$refs["editForm"].validate(function (valid) {
                    if (valid) {
                        if (self.editSearchDate && self.editSearchDate.length != 0) {
                            self.formData.startDate = self.editSearchDate[0];
                            self.formData.endDate = self.editSearchDate[1];
                        } else {
                            if (!self.formData.isLongActivity) {
                                self.$message({
                                    message: '非长期活动请选择日期',
                                    type: 'error'
                                });
                                return;
                            }
                            self.formData.startDate = "";
                            self.formData.endDate = "";
                        }
                        self.formData.content = UM.getEditor("content").getContent();
                        //titlePopu=0是新增,1是修改
                        if (self.titlePopu === 0) {
                            $.post(base + "/platform/cms/activity/add", self.formData, function (data) {
                                if (data.code === 0) {
                                    self.$message({
                                        message: data.msg,
                                        type: 'success'
                                    });
                                    self.editDialogVisible = false;
                                    self.isShow = true;
                                    self.editSearchDate = [];
                                    self.doSearch();
                                } else {
                                    self.$message({
                                        message: data.msg,
                                        type: 'error'
                                    });
                                }
                            }, "json");
                        } else {
                            $.post(base + "/platform/cms/activity/edit", self.formData, function (data) {
                                if (data.code === 0) {
                                    self.$message({
                                        message: data.msg,
                                        type: 'success'
                                    });
                                    self.editDialogVisible = false;
                                    self.isShow = true;
                                    self.editSearchDate = [];
                                    self.doSearch();
                                } else {
                                    self.$message({
                                        message: data.msg,
                                        type: 'error'
                                    });
                                }
                            }, "json");
                        }
                    }
                });
            },
            dropdownCommand: function (command) {//监听下拉框事件
                let self = this;
                // 禁用/启用 活动
                if ("disable" === command.type) {
                    $.post(base + "/platform/cms/activity/disabled", {
                        id: command.id,
                        status: command.status
                    }, function (data) {
                        if (data.code === 0) {
                            self.$message({
                                message: data.msg,
                                type: 'success'
                            });
                            self.doSearch();
                        } else {
                            self.$message({
                                message: data.msg,
                                type: 'error'
                            });
                        }
                    }, "json");
                }
                // 修改活动
                if ("edit" === command.type) {
                    self.formData = command.row;//加载数据
                    self.titlePopu = 1;
                    self.editDialogVisible = true;//打开编辑窗口
                    //打开窗口,表单先清空
                    if (self.$refs["editForm"]) {
                        self.$refs["editForm"].resetFields();
                    }
                    if (self.formData.isLongActivity) {
                        self.isShow = false;
                    } else {
                        self.editSearchDate = [command.row.startDate, command.row.endDate];
                    }
                    UM.getEditor("content");
                    setTimeout(function () {
                        UM.getEditor("content").setContent("" + self.formData.content, false);
                    }, 200);
                }
                // 删除活动
                if ("delete" === command.type) {
                    self.$confirm('此操作将删除这条活动', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                        callback: function (a, b) {
                            if ("confirm" === a) {//确认后再执行
                                $.post(base + "/platform/cms/activity/del", {id: command.id}, function (data) {
                                    if (data.code === 0) {
                                        self.$message({
                                            message: data.msg,
                                            type: 'success'
                                        });
                                        self.doSearch();
                                    } else {
                                        self.$message({
                                            message: data.msg,
                                            type: 'error'
                                        });
                                    }
                                }, "json");
                            }
                        }
                    });
                }
            },
            beforeUpload: function (file) {
                let isLt120M = file.size / 1024 / 1024 < 200;
                if (this.fileType.indexOf(file.name.substring(file.name.lastIndexOf(".") + 1, file.name.length)) < 0) {
                    this.$message.error('文件格式不符，仅可以上传' + this.fileType.join(','));
                    return false;
                }
                if (!isLt120M) {
                    this.$message.error('文件大小不能超过200MB');
                    return false;
                }
                return true;
            },
            // 图片上传
            handlePicSuccess: function (response, file, fileList, attrName) {// 上传图片
                if (response.code === 0) {
                    // 对象属性直接赋值不会触发试图更新，采用 Vue.set 、this.$set或者Object.assign({}，this.obj)创建新对象
                    // 或者也可以定义一个 refresh 属性 ，当数据发生改变时，该属性同时变化，也会触发视图更新
                    // this.formData.site_logo =response.data;
                    this.$set(this.formData, attrName, response.data);
                } else {
                    this.$set(this.formData, attrName, "");
                }
            },
            activityChange: function () {
                let self = this;
                if (self.formData.isLongActivity) {
                    self.isShow = false;
                } else {
                    self.isShow = true;
                }
            }
        },
        created: function () {
            this.pageData();
        }
    });
</script>
<!--#
}
#-->